package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.domain.SysSupplierProduct;
import com.ruoyi.system.mapper.SysProductMapper;
import com.ruoyi.system.mapper.SysSupplierProductMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysSupplierMapper;
import com.ruoyi.system.domain.SysSupplier;
import com.ruoyi.system.service.ISysSupplierService;

/**
 * 供应商信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-21
 */
@Service
public class SysSupplierServiceImpl implements ISysSupplierService {
    @Autowired
    private SysSupplierMapper sysSupplierMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysSupplierProductMapper sysSupplierProductMapper;
    @Autowired
    private SysProductMapper sysProductMapper;
    @Autowired
    private ISysUserService ISysUserService;

    /**
     * 查询供应商信息
     *
     * @param supplierId 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public SysSupplier selectSysSupplierBySupplierId(Long supplierId) {
        return sysSupplierMapper.selectSysSupplierBySupplierId(supplierId);
    }


    /**
     * 查询供应商信息列表
     * @param sysSupplier 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<SysSupplier> selectSysSupplierList(SysSupplier sysSupplier) {
        return sysSupplierMapper.selectSysSupplierList(sysSupplier);
    }

    /**
     * 新增供应商信息
     * 并且创建用户
     *
     * @param sysSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int insertSysSupplier(SysSupplier sysSupplier) {
        sysSupplierMapper.insertSysSupplier(sysSupplier);
        SysUser user = new SysUser();
        user.setUserName("supplier" + sysSupplier.getSupplierId());
        user.setSupplierId(sysSupplierMapper.selectSupplierIdBySysSupplier(sysSupplier));
        user.setNickName(sysSupplier.getSupplierName());
        user.setRoleIds(new Long[]{100L});
        user.setPassword(SecurityUtils.encryptPassword("123456"));
        user.setStatus("0");
        user.setCreateBy(SecurityUtils.getUsername());
        ISysUserService.insertUser(user);
        return 1;
    }

    /**
     * 修改供应商信息
     *
     * @param sysSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int updateSysSupplier(SysSupplier sysSupplier) {
        sysSupplierMapper.updateSysSupplier(sysSupplier);
        SysUser user = new SysUser();
        SysSupplier supplier = sysSupplierMapper.selectSysSupplierBySupplierId(sysSupplier.getSupplierId());
        user.setNickName(supplier.getSupplierName());
        user.setUserName("supplier" + sysSupplier.getSupplierId());
        user.setUpdateBy(SecurityUtils.getUsername());
        user.setUpdateTime(new Date());
        sysUserMapper.updateUser(user);
        return 1;
    }

    /**
     * 批量删除供应商信息
     * 并且删除对应的用户
     * @param supplierIds 需要删除的供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierBySupplierIds(Long[] supplierIds) {
        for (Long supplierId : supplierIds) {
            Long userId = sysSupplierMapper.selectUserIdBySupplierId(supplierId);
            sysUserMapper.deleteUserById(userId);
        }
        return sysSupplierMapper.deleteSysSupplierBySupplierIds(supplierIds);
    }

    /**
     * 删除供应商信息信息
     * 并且删除对应的用户
     * @param supplierId 供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierBySupplierId(Long supplierId) {
        Long userId = sysSupplierMapper.selectUserIdBySupplierId(supplierId);
        sysUserMapper.deleteUserById(userId);
        return sysSupplierMapper.deleteSysSupplierBySupplierId(supplierId);
    }
}
